package com.chenwang.measure.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenwang.measure.R;
import com.chenwang.measure.base.BaseActivity;
import com.chenwang.measure.model.Constant;
import com.chenwang.measure.model.ObserverOnNext;
import com.chenwang.measure.model.bean.LocationBean;
import com.chenwang.measure.model.bean.Task;
import com.chenwang.measure.model.bean.WorkBean;
import com.chenwang.measure.model.bean.WorkTaskBean;
import com.chenwang.measure.ui.adapter.MeasureAdapter;
import com.chenwang.measure.ui.view.TitleLayout;
import com.chenwang.measure.utils.DistanceUtils;
import com.chenwang.measure.utils.FileUtils;
import com.chenwang.measure.utils.MyCrashHandler;
import com.chenwang.measure.utils.MyLogger;
import com.chenwang.measure.utils.MyToast;
import com.chenwang.measure.utils.RxTime;
import com.chenwang.measure.utils.RxUtils;
import com.chenwang.measure.utils.SharePreferenceUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

//测量列表界面
public class MeasureActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.title)
    TitleLayout mTitle;    //标题
    @BindView(R.id.tv_setting)
    TextView mTvSetting;
    @BindView(R.id.tv_lat)
    TextView mTvLat;    //纬度
    @BindView(R.id.tv_lon)
    TextView mTvLon;   //经度
    @BindView(R.id.tv_height)
    TextView mTvHeight;  //高度
    @BindView(R.id.tv_satellite_total)
    TextView mTvSatelliteTotal; //卫星总数
    @BindView(R.id.tv_satellite)
    TextView mTvSatellite;  //参与解的卫星数
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.tv_location_status)
    TextView mTvLocationStatus;//定位状态 0未解算 16单点定位 17差分定位 50固定解
    private RxTime mRxTime;//定时器
    private MeasureAdapter mAdapter;
    private Task mTask; //当前任务
    private int mPosition;//列表当前点击的位置
    @BindView(R.id.tv_status)
    TextView mTvStatus;
    @Override
    public int setLayoutId() {
        return R.layout.activity_measure;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        showLoading("正在加载数据");
        initRecyclerView();
        initLocation();//获取定位
    }

    //更新经纬度等信息
    private void initLocation(){
        mRxTime = new RxTime();
        mRxTime.interval(1000, new RxTime.RxAction() {
            @Override
            public void action(long number) {
                if (LocationBean.getInstance().isState()){
                    mTvStatus.setText("设备已连接");
                }else {
                    mTvStatus.setText("设备未连接");

                }
                if (!TextUtils.isEmpty(LocationBean.getInstance().getLat()+"")) {
                    mTvLat.setText("纬度：" + LocationBean.getInstance().getLat());
                    mTvLon.setText("经度：" + LocationBean.getInstance().getLon());
                    mTvHeight.setText("高度：" + LocationBean.getInstance().getHight());
                    setLocationStatus(LocationBean.getInstance().getPos());
                    mTvSatelliteTotal.setText("卫星总数：" + LocationBean.getInstance().getSatelliteTotal());
                    mTvSatellite.setText("水平:" + DistanceUtils.decimalFour(LocationBean.getInstance().getHorizontal())+" 高:"+DistanceUtils.decimalFour(LocationBean.getInstance().getVertrcal()));

                }
            }
        });
    }

    //初始化任务段列表
    private void initRecyclerView(){
        // 1 代表任务未完成  2完成
       /* if (SharePreferenceUtils.getInt(this,"measure_finish" , 2) != 1){*/
        //如果为0 表示新任务
        if (getIntent().getIntExtra("state",-1 ) ==0){
            if (getIntent().getParcelableExtra("data")!= null){
                Observable.create(new ObservableOnSubscribe<Boolean>() {
                    @Override
                    public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                        WorkBean.OverviewDOListBean workTaskBean = getIntent().getParcelableExtra("data");
                        mTask = new Task(initWork(workTaskBean));

                        //遍历任务列表 将正在进行的任务状态设置1
                        for (int i = 0; i< WorkTaskBean.getInstance().getList().size() ;i++){
                            if (WorkTaskBean.getInstance().getList().get(i).getRwgsId()==mTask.getWorkTask().getRwgsId()){
                                mTask.getWorkTask().setMissionState(1);
                                WorkTaskBean.getInstance().getList().get(i).setMissionState(1);
                                break;
                            }

                        }
                        Gson gson = new Gson();
                        String jsont = gson.toJson(mTask);
                       // String taskjson = SharePreferenceUtils.getString(MeasureActivity.this,"measure_task" ,"" );
                        if (!TextUtils.isEmpty(jsont)){// 不等于空
                            String listjson = SharePreferenceUtils.getString(MeasureActivity.this,"measure_task_list" ,"" );
                            //Task task = gson.fromJson(taskjson,Task.class );
                            if (TextUtils.isEmpty(listjson)){//任务集合没有数据
                                 List<Task> tasks = new ArrayList<>();
                                 tasks.add(mTask);
                                 String json = gson.toJson(tasks);//将任务添加到集合保存
                                 SharePreferenceUtils.putString(MeasureActivity.this,"measure_task_list" ,json );
                            }else {
                                Type type = new TypeToken<List<Task>>(){}.getType();
                                List<Task> temp =gson.fromJson(listjson, type);
                                String taskjson = SharePreferenceUtils.getString(MeasureActivity.this,"measure_task" ,"" );
                                if ( !TextUtils.isEmpty(taskjson)) {
                                    Task task = gson.fromJson(taskjson,Task.class );
                                    for (int i = 0; i < temp.size(); i++) {
                                        if (temp.get(i).getWorkTask().getRwgsId()==task.getWorkTask().getRwgsId()) {
                                            temp.set(i, task);
                                            break;
                                        }
                                    }
                                }
                                temp.add(mTask);
                                String json = gson.toJson(temp);
                                SharePreferenceUtils.putString(MeasureActivity.this,"measure_task_list" ,json );
                            }
                        }
                        //生成json字符保存到sp中
                        SharePreferenceUtils.putString(MeasureActivity.this,"measure_task" ,jsont );//保存当前任务
                        SharePreferenceUtils.putInt(MeasureActivity.this, "measure_finish", 1);//保存任务状态
                        //保存将正在进行的任务保存到文件
                        emitter.onNext( FileUtils.writeFile(Constant.path+Constant.fileName, gson.toJson(WorkTaskBean.getInstance().getList()), false));

                    }
                }).compose(RxUtils.<Boolean>io_main())
                        .subscribe(new ObserverOnNext<Boolean>() {
                            @Override
                            public void onNext(Boolean bool) {
                                if (bool) {
                                    mTitle.setText(mTask.getWorkTask().getMissionName());//设置任务标题
                                    mRv.setLayoutManager(new LinearLayoutManager(MeasureActivity.this));
                                    mAdapter = new MeasureAdapter(mTask.getList());
                                    mAdapter.setOnItemClickListener(MeasureActivity.this);
                                    mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
                                    mRv.setAdapter(mAdapter);

                                }else {
                                    MyToast.show("加载失败");
                                }
                                dismissLoading();
                            }
                        });



            }else {
                MyToast.show("没有数据");
            }
        } else  if (getIntent().getIntExtra("state",-1 ) ==1){//进行中的任务
            int id = getIntent().getIntExtra("id",0);
            Gson gson = new Gson();
            //获取未完成的任务
            String taskjson = SharePreferenceUtils.getString(this,"measure_task" , "");
            Task task = gson.fromJson(taskjson,Task.class );
            if (!(task.getWorkTask().getRwgsId()==id)){
                String listjson = SharePreferenceUtils.getString(MeasureActivity.this, "measure_task_list", "");
                if (!TextUtils.isEmpty(listjson)) {
                    Type type = new TypeToken<List<Task>>() {
                    }.getType();
                    List<Task> temp = gson.fromJson(listjson, type);
                    for (int i = 0; i < temp.size(); i++) {
                        if (temp.get(i).getWorkTask().getRwgsId()==task.getWorkTask().getRwgsId()) {
                            temp.set(i, task);
                            String json = gson.toJson(temp);
                            SharePreferenceUtils.putString(MeasureActivity.this,"measure_task_list" ,json );
                            break;
                        }
                    }
                    for (int i = 0; i < temp.size(); i++) {
                        if (temp.get(i).getWorkTask().getRwgsId()==id) {

                            mTask = temp.get(i);
                            String json = gson.toJson(mTask);//生成json字符保存到sp中
                            SharePreferenceUtils.putString(MeasureActivity.this, "measure_task", json);
                            break;
                        }
                    }
                }
            }else {
                mTask = task;
            }
            mTitle.setText(mTask.getWorkTask().getMissionName());
            mRv.setLayoutManager(new LinearLayoutManager(this));
            mAdapter = new MeasureAdapter(mTask.getList());
            mAdapter.setOnItemClickListener(this);
            mRv.setAdapter(mAdapter);
            dismissLoading();
        } else {//从主界面打开的 上一次的任务

            initTask();
            Gson gson = new Gson();
            //获取未完成的任务
            String json = SharePreferenceUtils.getString(this,"measure_task" , "");
            if (TextUtils.isEmpty(json)){
                MyToast.show("没有测量测任务");
            }else {
                MyLogger.i(json);
                mTask = gson.fromJson(json, Task.class);
                mTitle.setText(mTask.getWorkTask().getMissionName());
                mRv.setLayoutManager(new LinearLayoutManager(this));
                mAdapter = new MeasureAdapter(mTask.getList());
                mAdapter.setOnItemClickListener(this);
                mRv.setAdapter(mAdapter);

            }
            dismissLoading();
       }

        mTitle.setClickListenertLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialogBack();
            }
        });
    }

    private void initTask() {
        Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                String json = FileUtils.readFile(Constant.path + Constant.fileName);
                if (TextUtils.isEmpty(json)){
                    emitter.onNext(false);
                }else {
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<WorkBean.OverviewDOListBean>>(){}.getType();
                    List<WorkBean.OverviewDOListBean> list =gson.fromJson(json, type);
                    WorkTaskBean.getInstance().setList(list );
                    emitter.onNext(true);

                }

            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ObserverOnNext<Boolean>() {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        dismissLoading();
                    }
                });


    }



    /**
     * 遍历所有任务将相同信号机赋值给当前任务
     * @param workTaskBean  任务
     * @return  遍历完相同点的任务
     */
    private WorkBean.OverviewDOListBean initWork(WorkBean.OverviewDOListBean workTaskBean){
        for (int i = 0; i < workTaskBean.getDetailedDOs().size(); i++) {
            for (WorkBean.OverviewDOListBean doListBean : WorkTaskBean.getInstance().getList()) {
                //2 是待上传  3是已完成
                if ((doListBean.getMissionState() == 2) || (doListBean.getMissionState() == 3)) {
                    for (WorkBean.OverviewDOListBean.DetailedDOsBean dOsBean : doListBean.getDetailedDOs()) {

                        //判断信号机编号是否相同
                        /*if (workTaskBean.getDetailedDOs().get(i).getSemaphore() == dOsBean.getSemaphore()) {
                            if (!TextUtils.isEmpty(dOsBean.getLonAndlat())) {//所有任务的点经纬度 不为空才赋值
                                workTaskBean.getDetailedDOs().get(i).setLonAndlat(dOsBean.getLonAndlat());
                            }
                        }*/
                        //遍历组id
                        if ((dOsBean.getGroupId() != null) && (dOsBean.getGroupId().size() != 0)) {
                            if ((workTaskBean.getDetailedDOs().get(i).getGroupId() != null) && (workTaskBean.getDetailedDOs().get(i).getGroupId().size() != 0)) {
                                for (int j = 0; j < dOsBean.getGroupId().size(); j++) {
                                    for (int k = 0; k < workTaskBean.getDetailedDOs().get(i).getGroupId().size(); k++) {
                                        if (dOsBean.getGroupId().get(j) == workTaskBean.getDetailedDOs().get(i).getGroupId().get(k)) {
                                            if (!TextUtils.isEmpty(dOsBean.getLonAndlat())) {//所有任务的点经纬度 不为空才赋值
                                                workTaskBean.getDetailedDOs().get(i).setLonAndlat(dOsBean.getLonAndlat());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return workTaskBean;
    }


    @OnClick({R.id.tv_setting,R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_save://完成保存当前任务
                showSaveDialog();
                break;
            case R.id.tv_setting://设置测量参数
                startActivity(new Intent(this, MeasureSettingActivity.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRxTime !=null){
            mRxTime.cancel();
        }

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        //点 位是否测量过  0 未   1 开始  2结束  3点都测量过 4完成
        mPosition = position;
        mTask.getList().get(position).setPosition(mPosition);
        switch (mTask.getList().get(position).getState()){
            case 0://测量
                Intent intent = new Intent(this,MeasureDetailActivity.class);
                intent.putExtra("data",mTask.getList().get(mPosition) );
                //intent.putIntegerArrayListExtra("start", (ArrayList<Integer>) mTask.getList().get(mPosition).getStatr().getGroupId());
               // intent.putIntegerArrayListExtra("end", (ArrayList<Integer>) mTask.getList().get(mPosition).getEnd().getGroupId());

                startActivityForResult(intent, 1);
                break;
            case 1://开始点已测量
                showMeasureStartDialog(mTask.mList.get(position));
                break;
            case 2://结束点已测量
                showMeasureEndDialog(mTask.mList.get(position));
                break;
            case 3://两点都是 赋值过的
                showMeasureDialog(mTask.mList.get(position));
                break;
            case 4://重新测量
                showMeasureDialog(mTask.mList.get(position));
                break;

        }

    }
    //开始点已测
    private void showMeasureStartDialog(final Task.Segment segment){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("测量");
        normalDialog.setMessage("信号机:"+ segment.getStatr().getSemaphoreName()+"已测量过 请直接测量信号机："+segment.getEnd().getSemaphoreName());
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MeasureActivity.this,MeasureDetailActivity.class);
                        intent.putExtra("data",segment);
                        //intent.putIntegerArrayListExtra("start", (ArrayList<Integer>) mTask.getList().get(mPosition).getStatr().getGroupId());
                       // intent.putIntegerArrayListExtra("end", (ArrayList<Integer>) mTask.getList().get(mPosition).getEnd().getGroupId());
                        startActivityForResult(intent, 1);

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();
    }
    //结束点已测
    private void showMeasureEndDialog(final Task.Segment segment){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("测量");
        normalDialog.setMessage("信号机:"+ segment.getEnd().getSemaphoreName()+"已测量过 请直接测量信号机："+segment.getStatr().getSemaphoreName());
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MeasureActivity.this,MeasureDetailActivity.class);
                      //  intent.putIntegerArrayListExtra("start", (ArrayList<Integer>) mTask.getList().get(mPosition).getStatr().getGroupId());
                      //  intent.putIntegerArrayListExtra("end", (ArrayList<Integer>) mTask.getList().get(mPosition).getEnd().getGroupId());
                        intent.putExtra("data",segment);
                        startActivityForResult(intent, 1);

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();
    }

    //保存
    private void showSaveDialog(){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("测量完成");
        normalDialog.setMessage("是否保存该任务");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       showLoading("正在保存");
                        Observable.create(new ObservableOnSubscribe<Boolean>() {
                            @Override
                            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                                for (int z = 0; z < mTask.getList().size(); z++) {
                                    if (mTask.mList.get(z).getState() != 5) {// 手动输入的结果 不遍历
                                        for (int i = 0; i < WorkTaskBean.getInstance().getList().size(); i++) {
                                            //只遍历   2是代上传   将组id相同的 信号机 经纬度 赋值
                                            if (WorkTaskBean.getInstance().getList().get(i).getMissionState() == 2) {//不遍历当前任务
                                                if (!(WorkTaskBean.getInstance().getList().get(i).getRwgsId()==mTask.getWorkTask().getRwgsId())) {
                                                    //遍历所有任务
                                                    for (int j = 0; j < WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().size(); j++) {
                                                        //查找一样的开始 信号机名字
/*
                                                        if (mTask.getList().get(z).getStatr().getSemaphore() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getSemaphore()) {
                                                            //经纬度不一样 代表点位是重新测量过的
                                                            if (mTask.getList().get(z).getStatr().getLonAndlat().equals(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat())) {

                                                            } else {
                                                                WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setLonAndlat(mTask.getList().get(z).getStatr().getLonAndlat());
                                                                for (int k = 0; k < WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().size(); k++) {
                                                                    //查找 任务段另一个点位
                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLast() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {
                                                                        //
                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                        //  WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                    }
                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getNext() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {
                                                                        //
                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                        //WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                    }
                                                                }
                                                            }
                                                        }
*/
                                                        //查找信号机相同点并赋值  组id查找
                                                        if ((mTask.getList().get(z).getStatr().getGroupId() != null) && (mTask.getList().get(z).getStatr().getGroupId().size() != 0)) {
                                                            if ((WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId() != null) && (
                                                                    WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId().size() != 0
                                                            )) {
                                                                for (int a = 0; a < mTask.getList().get(z).getStatr().getGroupId().size(); a++) {
                                                                    for (int b = 0; b < WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId().size(); b++) {
                                                                        if (mTask.getList().get(z).getStatr().getGroupId().get(a) == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId().get(b)) {
                                                                            if (mTask.getList().get(z).getStatr().getLonAndlat().equals(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat())) {

                                                                            } else {
                                                                                WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setLonAndlat(mTask.getList().get(z).getStatr().getLonAndlat());
                                                                                for (int k = 0; k < WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().size(); k++) {
                                                                                    //查找 任务段另一个点位
                                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLast() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {
                                                                                        //
                                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                                        //  WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                                    }
                                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getNext() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {
                                                                                        //
                                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                                        //WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                       /* if (mTask.getList().get(z).getEnd().getSemaphore() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getSemaphore()) {
                                                            if (mTask.getList().get(z).getEnd().getLonAndlat().equals(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat())) {

                                                            } else {
                                                                WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setLonAndlat(mTask.getList().get(z).getStatr().getLonAndlat());
                                                                for (int k = 0; k < WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().size(); k++) {

                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLast() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {

                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                        //WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                    }
                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getNext() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {

                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                        // WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                    }
                                                                }
                                                            }
                                                        }*/
                                                        //查找信号机相同点并赋值  组id查找
                                                        if ((mTask.getList().get(z).getEnd().getGroupId() != null) && (mTask.getList().get(z).getEnd().getGroupId().size() != 0)) {
                                                            if ((WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId() != null) && (
                                                                    WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId().size() != 0
                                                            )) {
                                                                for (int a = 0; a < mTask.getList().get(z).getEnd().getGroupId().size(); a++) {
                                                                    for (int b = 0; b < WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId().size(); b++) {
                                                                        if (mTask.getList().get(z).getEnd().getGroupId().get(a) == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getGroupId().get(b)) {
                                                                            if (mTask.getList().get(z).getEnd().getLonAndlat().equals(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat())) {

                                                                            } else {
                                                                                WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setLonAndlat(mTask.getList().get(z).getStatr().getLonAndlat());
                                                                                for (int k = 0; k < WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().size(); k++) {

                                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLast() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {

                                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                                        //WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                                    }
                                                                                    if (WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getNext() == WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getSemaphore()) {

                                                                                        double d = DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).getLonAndlat()),
                                                                                                DistanceUtils.getLon(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat()), DistanceUtils.getLat(WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).getLonAndlat())));
                                                                                        WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(j).setSemaphoreSpace(d + "");
                                                                                        // WorkTaskBean.getInstance().getList().get(i).getDetailedDOs().get(k).setSemaphoreSpace(d + "");
                                                                                    }
                                                                                }
                                                                            }


                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                            }
                                        }
                                    }
                                }
                                //任务状态设为待上传
                                for (int i = 0; i< WorkTaskBean.getInstance().getList().size() ;i++){
                                    if (WorkTaskBean.getInstance().getList().get(i).getRwgsId()==mTask.getWorkTask().getRwgsId()){
                                        mTask.getWorkTask().setMissionState(2);
                                        WorkTaskBean.getInstance().getList().set(i, mTask.getWork());
                                        break;
                                    }

                                }
                                //移除测量完的任务 保存sp中
                                Gson gson = new Gson();
                                String listjson = SharePreferenceUtils.getString(MeasureActivity.this,"measure_task_list" ,"" );
                                if (!TextUtils.isEmpty(listjson)){
                                    Type type = new TypeToken<List<Task>>(){}.getType();
                                    List<Task> temp =gson.fromJson(listjson, type);
                                    String taskjson = SharePreferenceUtils.getString(MeasureActivity.this,"measure_task" ,"" );
                                    Task task = gson.fromJson(taskjson,Task.class );
                                    for (int i =0;i<temp.size();i++){
                                        if (temp.get(i).getWorkTask().getRwgsId()==task.getWorkTask().getRwgsId()){
                                            temp.remove(i);
                                            String json = gson.toJson(temp);
                                            SharePreferenceUtils.putString(MeasureActivity.this,"measure_task_list" ,json );
                                            break;

                                        }

                                    }
                                }
                                // 1任务未完成  2已完成;
                                SharePreferenceUtils.putInt(MeasureActivity.this, "measure_finish", 2);
                                Boolean bool = FileUtils.writeFile(Constant.path+Constant.fileName, gson.toJson(WorkTaskBean.getInstance().getList()), false);
                                System.out.println(bool);
                                emitter.onNext(bool );

                            }
                        }).subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new ObserverOnNext<Boolean>() {
                                    @Override
                                    public void onNext(Boolean aBoolean) {
                                        dismissLoading();
                                        if (aBoolean){

                                            MyToast.show("保存成功");
                                            finish();
                                        }else {
                                            MyToast.show("保存失败");
                                        }
                                    }
                                });

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();
    }

    //测量完成
    private void showMeasureDialog(final Task.Segment segment){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("测量完成");
        normalDialog.setMessage("该段已测量完成 是否重新测量");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MeasureActivity.this,MeasureDetailActivity.class);
                        intent.putExtra("data",segment);
                       // intent.putIntegerArrayListExtra("start", (ArrayList<Integer>) mTask.getList().get(mPosition).getStatr().getGroupId());
                       // intent.putIntegerArrayListExtra("end", (ArrayList<Integer>) mTask.getList().get(mPosition).getEnd().getGroupId());
                        startActivityForResult(intent, 1);

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();
    }


    //设置定位状态
    private void setLocationStatus(int status){
        switch (status){
            case 0:
                mTvLocationStatus.setText("未解算: 0");
                break;
            case 16:
                mTvLocationStatus.setText("单点定位: 16");
                break;
            case 17:
                mTvLocationStatus.setText("差分定位: 17");
                break;
            case 50:
                mTvLocationStatus.setText("固定解: 50");
                break;
            case 18:
                mTvLocationStatus.setText("SBAS: 18");
                break;
            case 34:
                mTvLocationStatus.setText("浮点解: 34");
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == 1){
                if (resultCode == 2){//MeasureDetailActivity中返回的
                    Task.Segment segment = data.getParcelableExtra("data");//获取测量完成的数据

                    List<Integer> start = data.getIntegerArrayListExtra("start");//--可注释掉
                    List<Integer> end = data.getIntegerArrayListExtra("end");//--可注释掉
                    segment.getEnd().setGroupId(end);//--可注释掉
                    segment.getStatr().setGroupId(start);//--可注释掉


                    MyLogger.i(segment.getStatr().getSemaphoreSpace()+"--"+segment.getEnd().getSemaphoreSpace());
                    mPosition = segment.getPosition();
                    mTask.getList().set(mPosition,segment );
                    //0 未   1 开始  2结束  3 2点都测量过 4完成
                    switch (segment.getState()){
                        case 5://手动输入的结果 没有经纬度
                            break;
                        case 0: //未测量
                            segment.setState(4);
                            //遍历相同信号机赋值
                            for (int i = 0; i < mTask.getList().size(); i++) {
                                /* if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())){*/
                                if (i != mPosition) {//排除当前测量段
                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment开始点
                                    if (mTask.getList().get(i).getStatr().getSemaphore() == segment.getStatr().getSemaphore()) {
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {//当前任务点经纬度为空
                                            if (mTask.getList().get(i).getState() == 2) {//如果等于2  代表 结束点有值  算出两点距离
                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                                mTask.getList().get(i).setState(1);
                                            }

                                        }
                                        //查找信号机相同点并赋值  组id查找
                                        if ((mTask.getList().get(i).getStatr().getGroupId() != null) && (mTask.getList().get(i).getStatr().getGroupId().size() != 0)) {
                                            if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {
                                                            if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {//当前任务点经纬度为空
                                                                if (mTask.getList().get(i).getState() == 2) {//如果等于2  代表 结束点有值  算出两点距离
                                                                    mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                                                    mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                            , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                    mTask.getList().get(i).setState(3);
                                                                } else {
                                                                    mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                                                    mTask.getList().get(i).setState(1);
                                                                }

                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment结束点
                                    if (mTask.getList().get(i).getStatr().getSemaphore() == segment.getEnd().getSemaphore()) {
                                        //  mTask.getList().get(i).getStatr().setImageUrl(segment.getEnd().getImageUrl());
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                            if (mTask.getList().get(i).getState() == 2) {
                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                mTask.getList().get(i).setState(1);
                                            }
                                        }
                                        //查找信号机相同点并赋值  组id查找
                                        if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                            for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                    if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                        if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                                            if (mTask.getList().get(i).getState() == 2) {
                                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                mTask.getList().get(i).setState(3);
                                                            } else {
                                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                mTask.getList().get(i).setState(1);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment结束点
                                    if (mTask.getList().get(i).getEnd().getSemaphore() == segment.getEnd().getSemaphore()) {
                                        //mTask.getList().get(i).getEnd().setImageUrl(segment.getEnd().getImageUrl());
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            if (mTask.getList().get(i).getState() == 1) {
                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                mTask.getList().get(i).setState(2);
                                            }
                                        }
                                        //查找信号机相同点并赋值  组id查找
                                        if ((mTask.getList().get(i).getEnd().getGroupId() != null) && (mTask.getList().get(i).getEnd().getGroupId().size() != 0)) {
                                            if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                            if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                                if (mTask.getList().get(i).getState() == 1) {
                                                                    mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                    mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                            , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                    mTask.getList().get(i).setState(3);
                                                                } else {
                                                                    mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                    mTask.getList().get(i).setState(2);
                                                                }
                                                            }
                                                        }


                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment开始点
                                    if (mTask.getList().get(i).getEnd().getSemaphore() == segment.getStatr().getSemaphore()) {
                                        //mTask.getList().get(i).getEnd().setImageUrl(segment.getStatr().getImageUrl());
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            if (mTask.getList().get(i).getState() == 1) {
                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                                mTask.getList().get(i).setState(2);
                                            }
                                        }
                                        //查找信号机相同点并赋值  组id查找
                                        if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                            for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                    if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {
                                                        if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                            if (mTask.getList().get(i).getState() == 1) {
                                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                        , DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                mTask.getList().get(i).setState(3);
                                                            } else {
                                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                                                mTask.getList().get(i).setState(2);
                                                            }
                                                        }
                                                    }

                                                }
                                            }
                                        }
                                    }


                                }
                            }
                            mTask.getList().get(mPosition).setState(4);//测量完成
                            break;
                        case 1://开始点
                            for (int i = 0; i < mTask.getList().size(); i++) {
                                if (i != mPosition) {
                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment结束点
                                    if (mTask.getList().get(i).getEnd().getSemaphore() == segment.getEnd().getSemaphore()) {
                                        // mTask.getList().get(i).getEnd().setImageUrl(segment.getEnd().getImageUrl());
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                            if (mTask.getList().get(i).getState() == 1) {
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).setState(2);
                                            }
                                        }
                                        if ((mTask.getList().get(i).getEnd().getGroupId() != null) && (mTask.getList().get(i).getEnd().getGroupId().size() != 0)) {
                                            if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                            if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                if (mTask.getList().get(i).getState() == 1) {
                                                                    mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                            , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                    mTask.getList().get(i).setState(3);
                                                                } else {
                                                                    mTask.getList().get(i).setState(2);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment结束点
                                    if (mTask.getList().get(i).getStatr().getSemaphore() == segment.getEnd().getSemaphore()) {
                                        //  mTask.getList().get(i).getStatr().setImageUrl(segment.getEnd().getImageUrl());
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                            mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                            if (mTask.getList().get(i).getState() == 2) {
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).setState(1);
                                            }
                                        }
                                        if ((mTask.getList().get(i).getStatr().getGroupId() != null) && (mTask.getList().get(i).getStatr().getGroupId().size() != 0)) {
                                            if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                            if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                if (mTask.getList().get(i).getState() == 2) {
                                                                    mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                            , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                    mTask.getList().get(i).setState(3);
                                                                } else {
                                                                    mTask.getList().get(i).setState(1);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            mTask.getList().get(mPosition).setState(4);//测量完成
                            break;
                        case 2://结束点
                            for (int i = 0; i < mTask.getList().size(); i++) {
                                if (i != mPosition) {
                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment开始点
                                    if (mTask.getList().get(i).getEnd().getSemaphore() == segment.getStatr().getSemaphore()) {
                                        // mTask.getList().get(i).getEnd().setImageUrl(segment.getStatr().getImageUrl());
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                            if (mTask.getList().get(i).getState() == 1) {
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).setState(2);
                                            }
                                        }
                                        if ((mTask.getList().get(i).getEnd().getGroupId() != null) && (mTask.getList().get(i).getEnd().getGroupId().size() != 0)) {
                                            if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {
                                                            if (TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                                mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                if (mTask.getList().get(i).getState() == 1) {
                                                                    mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                            , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                    mTask.getList().get(i).setState(3);
                                                                } else {
                                                                    mTask.getList().get(i).setState(2);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment开始点
                                    if (mTask.getList().get(i).getStatr().getSemaphore() == segment.getStatr().getSemaphore()) {
                                        //mTask.getList().get(i).getStatr().setImageUrl(segment.getStatr().getImageUrl());
                                        if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                            mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                            if (mTask.getList().get(i).getState() == 2) {
                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                        , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                mTask.getList().get(i).setState(3);
                                            } else {
                                                mTask.getList().get(i).setState(1);
                                            }
                                        }
                                        if ((mTask.getList().get(i).getStatr().getGroupId() != null) && (mTask.getList().get(i).getStatr().getGroupId().size() != 0)) {
                                            if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {
                                                            if (TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                                                mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                                if (mTask.getList().get(i).getState() == 2) {
                                                                    mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat())
                                                                            , DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()), DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                                    mTask.getList().get(i).setState(3);
                                                                } else {
                                                                    mTask.getList().get(i).setState(1);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            mTask.getList().get(mPosition).setState(4);//测量完成
                            break;
                        case 3://点位 是算出来的  重新测量
                            mTask.getList().set(mPosition,segment );
                            for (int i = 0; i < mTask.getList().size(); i++) {
                                if (mPosition != i) {
                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment开始点
                                    if (segment.getStatr().getSemaphore() == mTask.getList().get(i).getStatr().getSemaphore()) {
                                        //mTask.getList().get(i).getStatr().setImageUrl(segment.getStatr().getImageUrl());
                                        mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                        }
                                        if ((mTask.getList().get(i).getStatr().getGroupId() != null) && (mTask.getList().get(i).getStatr().getGroupId().size() != 0)) {
                                            if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {
                                                            mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment开始点
                                    if (segment.getStatr().getSemaphore()== mTask.getList().get(i).getEnd().getSemaphore()) {
                                        // mTask.getList().get(i).getEnd().setImageUrl(segment.getStatr().getImageUrl());
                                        mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                        }
                                        if ((mTask.getList().get(i).getEnd().getGroupId() != null) && (mTask.getList().get(i).getEnd().getGroupId().size() != 0)) {
                                            if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {
                                                            mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment结束点
                                    if (segment.getEnd().getSemaphore() == mTask.getList().get(i).getStatr().getSemaphore()) {
                                        // mTask.getList().get(i).getStatr().setImageUrl(segment.getEnd().getImageUrl());
                                        mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                        }
                                        if ((mTask.getList().get(i).getStatr().getGroupId() != null) && (mTask.getList().get(i).getStatr().getGroupId().size() != 0)) {
                                            if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                            mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment结束点
                                    if (segment.getEnd().getSemaphore() == mTask.getList().get(i).getEnd().getSemaphore()) {
                                        // mTask.getList().get(i).getEnd().setImageUrl(segment.getEnd().getImageUrl());
                                        mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                        }
                                        if ((mTask.getList().get(i).getEnd().getGroupId() != null) && (mTask.getList().get(i).getEnd().getGroupId().size() != 0)) {
                                            if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                            mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            // 0  1  2 3 新  进行  待上传 完成

                            mTask.getList().get(mPosition).setState(4);//测量完成
                            break;
                        case 4://重新测量
                            mTask.getList().set(mPosition,segment );
                            for (int i = 0; i < mTask.getList().size(); i++) {
                                if (mPosition != i) {

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment开始点
                                    if (segment.getStatr().getSemaphore() == mTask.getList().get(i).getStatr().getSemaphore()) {
                                        // mTask.getList().get(i).getStatr().setImageUrl(segment.getStatr().getImageUrl());
                                        mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                        }
                                        if ((mTask.getList().get(i).getStatr().getGroupId() != null) && (mTask.getList().get(i).getStatr().getGroupId().size() != 0)) {
                                            if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {

                                                            mTask.getList().get(i).getStatr().setLonAndlat(segment.getStatr().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                            }
                                                        }
                                                    }

                                                }

                                            }

                                        }

                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment开始点
                                    if (segment.getStatr().getSemaphore() == mTask.getList().get(i).getEnd().getSemaphore()) {
                                        //  mTask.getList().get(i).getEnd().setImageUrl(segment.getStatr().getImageUrl());
                                        mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                       /* System.out.println(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))) + "pppppppp");*/
                                        }
                                        if ((mTask.getList().get(i).getEnd().getGroupId() != null) && (mTask.getList().get(i).getEnd().getGroupId().size() != 0)) {
                                            if ((segment.getStatr().getGroupId() != null) && (segment.getStatr().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getStatr().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getStatr().getGroupId().get(b)) {
                                                            mTask.getList().get(i).getEnd().setLonAndlat(segment.getStatr().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                       /*     System.out.println(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))) + "pppppppp");*/
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有开始点与segment结束点
                                    if (segment.getEnd().getSemaphore() == mTask.getList().get(i).getStatr().getSemaphore()) {
                                        // mTask.getList().get(i).getStatr().setImageUrl(segment.getEnd().getImageUrl());
                                        mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                        /*System.out.println(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))) + "pppppppplll");*/
                                        }
                                        if ((mTask.getList().get(i).getStatr().getGroupId() != null) && (mTask.getList().get(i).getStatr().getGroupId().size() != 0)) {
                                            if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getStatr().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getStatr().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                            mTask.getList().get(i).getStatr().setLonAndlat(segment.getEnd().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getEnd().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                           /* System.out.println(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))) + "pppppppplll");*/
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    //查找信号机相同点并赋值  信号机id查找   遍历所有结束点与segment结束点
                                    if (segment.getEnd().getSemaphore()==mTask.getList().get(i).getEnd().getSemaphore()) {
                                        mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                        //mTask.getList().get(i).getEnd().setImageUrl(segment.getEnd().getImageUrl());
                                        //System.out.println(mTask.getList().get(i).getStatr().getLonAndlat() + "ppppp" + mTask.getList().get(i).getEnd().getLonAndlat());
                                        if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                            mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                     /*   System.out.println(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))) + "pppppppllllkkp");*/
                                        }
                                        if ((mTask.getList().get(i).getEnd().getGroupId() != null) && (mTask.getList().get(i).getEnd().getGroupId().size() != 0)) {
                                            if ((segment.getEnd().getGroupId() != null) && (segment.getEnd().getGroupId().size() != 0)) {
                                                for (int a = 0; a < mTask.getList().get(i).getEnd().getGroupId().size(); a++) {
                                                    for (int b = 0; b < segment.getEnd().getGroupId().size(); b++) {
                                                        if (mTask.getList().get(i).getEnd().getGroupId().get(a) == segment.getEnd().getGroupId().get(b)) {
                                                            mTask.getList().get(i).getEnd().setLonAndlat(segment.getEnd().getLonAndlat());
                                                            // System.out.println(mTask.getList().get(i).getStatr().getLonAndlat() + "ppppp" + mTask.getList().get(i).getEnd().getLonAndlat());
                                                            if (!TextUtils.isEmpty(mTask.getList().get(i).getStatr().getLonAndlat())) {
                                                                mTask.getList().get(i).setDistance(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                        DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                        DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))));
                                                          /*  System.out.println(DistanceUtils.decimalTwo(DistanceUtils.getDistance(
                                                                    DistanceUtils.getLon(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                    DistanceUtils.getLat(mTask.getList().get(i).getStatr().getLonAndlat()),
                                                                    DistanceUtils.getLon(mTask.getList().get(i).getEnd().getLonAndlat()),
                                                                    DistanceUtils.getLat(mTask.getList().get(i).getEnd().getLonAndlat()))) + "pppppppllllkkp");*/
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }


                            }


                            break;

                    }
                    mAdapter.replaceData(mTask.getList());//刷新列表
                    Gson gson = new Gson();
                    String json = gson.toJson(mTask);
                    SharePreferenceUtils.putString(this,"measure_task" , json);//将数据保存sp中
                }
            }
        }catch (Exception e){
            MyCrashHandler.newInstance().saveCrashInfoToFile(e);

        }

    }

    @Override
    public void onBackPressed() {
        showDialogBack();
    }

    //显示退出对话框
    private void showDialogBack(){

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("退出");
        normalDialog.setMessage("是否退出");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //退出保存json到sp中
                        Gson gson = new Gson();
                        String json = gson.toJson(mTask);
                        SharePreferenceUtils.putString(MeasureActivity.this,"measure_task" , json);
                        finish();

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }
}
